package han.chensing.latos.common.level.content.timeLine.motion;

import java.io.Serial;
import java.io.Serializable;

import han.chensing.latos.common.level.content.timeLine.motion.interpolator.Bezier2Interpolator;
import han.chensing.latos.common.level.content.timeLine.motion.interpolator.Interpolator;
import han.chensing.latos.common.level.content.timeLine.frame.GeneralEventTypes;
import han.chensing.latos.common.level.content.timeLine.frame.FrameGeneral;
import han.chensing.latos.common.level.util.MathUtil;

public class ValueMotion implements Serializable {
    @Serial
    private static final long serialVersionUID = -7564966980480883953L;

    protected GeneralEventTypes type;
    protected float startValue;
    protected float endValue;
    protected float direction;
    protected boolean originStart;
    protected boolean originEnd;
    protected float duration;
    protected Interpolator interpolator;

    public static ValueMotion makeLineAnimation(FrameGeneral[] sideFrames, GeneralEventTypes type){
        FrameGeneral frame1 = sideFrames[0];
        FrameGeneral frame2 = sideFrames[1];
        float duration = frame2.getTime() - frame1.getTime();
        float start;
        float end;
        switch (type) {
            case X -> {
                start = frame1.getX();
                end = frame2.getX();
            }
            case Y -> {
                start = frame1.getY();
                end = frame2.getY();
            }
            case Rotate -> {
                start = frame1.getRotate();
                end = frame2.getRotate();
            }
            case Alpha -> {
                start = frame1.getAlpha();
                end = frame2.getAlpha();
            }
            case Speed-> {
                start = frame1.getSpeed();
                end = frame2.getSpeed();
            }
            default -> start=end=0;
        }
        ValueMotion valueMotion =new ValueMotion();
        valueMotion.setDuration(duration);
        valueMotion.setStartValue(start);
        valueMotion.setEndValue(end);
        valueMotion.setType(type);
        return valueMotion;
    }

    public ValueMotion(){
        this.interpolator=new Bezier2Interpolator(.5f,.5f,.5f,.5f);
    }

    public float getValue(float relativeTime){
        relativeTime= MathUtil.range(0,relativeTime,1);
        return getInterpolator().value(relativeTime)
                * (getEndValue() - getStartValue());
    }

    public GeneralEventTypes getType() {
        return type;
    }

    public void setType(GeneralEventTypes type) {
        this.type = type;
    }

    public float getStartValue() {
        return startValue;
    }

    public void setStartValue(float startValue) {
        this.startValue = startValue;
    }

    public float getEndValue() {
        return endValue;
    }

    public void setEndValue(float endValue) {
        this.endValue = endValue;
    }

    public boolean isOriginStart() {
        return originStart;
    }

    public void setOriginStart(boolean originStart) {
        this.originStart = originStart;
    }

    public boolean isOriginEnd() {
        return originEnd;
    }

    public void setOriginEnd(boolean originEnd) {
        this.originEnd = originEnd;
    }

    public float getDirection() {
        return direction;
    }

    public void setDirection(float direction) {
        this.direction = direction;
    }

    public float getDuration() {
        return duration;
    }

    public void setDuration(float duration) {
        this.duration = duration;
    }

    public Interpolator getInterpolator() {
        return interpolator;
    }

    public void setInterpolator(Interpolator interpolator) {
        this.interpolator = interpolator;
    }
}
